﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

//Mark Rowe 
//From the IRDev Library Project.
namespace AutoDeploy.Common.Library
{
    public static class StringHelper
    {

        public static bool ListValueMatch(string s, List<string> ls, bool caseSensitive)
        {
            bool isMatch = false;
            foreach (string sd in ls)
            {
                if (!caseSensitive)
                {
                    isMatch = (s.ToLower().Trim() == sd.ToLower().Trim());
                }
                else
                {
                    isMatch = (s.Trim() == sd.Trim());
                }
                if (isMatch)
                {
                    if (sd != "\n")
                    {
                        return isMatch;
                    }
                }
            }
            return isMatch;
        }

        public static bool ListValueContains(string s, List<string> ls, bool caseSensitive)
        {
            bool isContained = false;
            foreach (string sd in ls)
            {
                if (!caseSensitive)
                {
                    isContained = s.ToLower().Contains(sd.ToLower().Trim());
                }
                else
                {
                    isContained = s.Contains(sd.Trim());
                }
                if (isContained)
                {
                    if (sd != "\n")
                    {
                        return isContained;
                    }
                }
            }
            return isContained;
        }

        public static string ListOfStringToString(List<string> ls, bool newLineForEachString)
        {
            StringBuilder sb = new StringBuilder();
            foreach (string s in ls)
            {
                sb.Append(newLineForEachString ? s + "\n" : s);
            }
            return sb.ToString();
        }

        public static List<string> StringToListOfString(string s, string delimeter, bool trim, bool removeEmpty)
        {
            List<string> ls = new List<string>();
            //My parser doesnt seem to handle escape sequences properly
            #region Special
            if (delimeter == "\n")
            {
                return s.Split('\n').ToList();
            }
            if (delimeter == "\t")
            {
                return s.Split('\t').ToList();
            }
            if (delimeter == "\r")
            {
                return s.Split('\r').ToList();
            }
            //If single char use Defualt C# Split. 
            if (delimeter.Trim().Length == 1)
            {
                return s.Split(char.Parse(delimeter[0].ToString())).ToList();
            }
            #endregion Special
            try
            {
                int IndexOfDelimeter = 0;
                int DelimeterLength = delimeter.Length;
                string Argument = "";
                string remaining = s;

                if (delimeter.Trim().Length == 0 || !s.Contains(delimeter))
                {
                    ls.Add(s);
                    return ls;
                }
                do
                {
                    IndexOfDelimeter = remaining.IndexOf(delimeter);
                    if (IndexOfDelimeter == -1)
                    {
                        ls.Add(remaining);
                        return ls;
                    }
                    else
                    {
                        if (IndexOfDelimeter < 1)
                        {
                            if (!removeEmpty)
                            {
                                ls.Add("");
                            }
                            remaining = remaining.Substring(1);
                        }
                        else
                        {
                            Argument = remaining.Substring(0, IndexOfDelimeter);
                            if (removeEmpty && Argument.Trim().Length == 0)
                            {

                            }
                            else
                            {
                                if (trim)
                                {
                                    ls.Add(Argument.Trim());
                                }
                                else
                                {
                                    ls.Add(Argument);
                                }
                            }
                            remaining = remaining.Substring(IndexOfDelimeter + DelimeterLength);
                        }
                    }
                }
                while (remaining.Length > 0);
                return ls;
            }
            catch (Exception ex)
            {
            }
            return ls;
        }

        public static string FormatAutoDeployValue(string s, Dictionary<string,string> preparedCommands, ref List <string> Errors)
        {
            //If it has a special pre-command
            if (s.Contains("::") && s.StartsWith("["))
            {
                List<string> ls = AutoDeploy.Common.Library.StringHelper.StringToListOfString(s.Substring(0, s.IndexOf("]")).Replace("[", "").Replace("]", ""), "::",true, false);
                switch (ls[0].Trim())
                {
                    case "PreparedApp":
                        {
                            if (preparedCommands.ContainsKey(ls[1]))
                            {
                                s = s.Replace(s.Substring(0,s.IndexOf("]")+1), " " + preparedCommands[ls[1]]);
                            }
                            else
                            {
                                Errors.Add(string.Format("PreparedApp:{0} not found.\nOriginalString:{1}\n", ls[1], s));
                                s = string.Empty;
                            }
                            break;
                        }
                }

            }
            if (s.Contains("[Program Files]"))
            {
                s = ExternalHelper.Helper.CheckForFileLocation(s, ref Errors);
            }

            return s;
        }

    }
}
